package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ahpu.propertymanage.dataclass.PageSet;
import edu.ahpu.propertymanage.pojo.*;
import edu.ahpu.propertymanage.service.*;
import edu.ahpu.propertymanage.util.GetID;
import edu.ahpu.propertymanage.util.SaveOwner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/owner")
public class OwnerController {

    @Autowired
    OwnerService ownerService;

    @Autowired
    PropertyTageService propertyTageService;

    @Autowired
    GetID getID;

    @Autowired
    EleCostService eleCostService;

    @Autowired
    WaterCostService waterCostService;

    @Autowired
    GasCostServie gasCostServie;

    @Autowired
    PropCostService propCostService;

    @Autowired
    ManageCarService manageCarService;

    @Autowired
    SaveOwner saveOwner;

    @PostMapping("/updateowner")
    public String updataOwner(@RequestBody Owner owner) {
        if (owner.getEleFId() == null) {
            boolean bool = ownerService.updateById(owner);
            if (bool)
                return "success";
            else
                return "error";
        } else {
            boolean bool = ownerService.updateById(owner);
            if (bool)
                return "success";
            else
                return "error";
        }
    }

    @PostMapping("saveowner")
    public String saveOwner(@RequestBody Owner owner) {

        boolean bool = saveOwner.saveOwner(owner);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("ownerpage")
    public IPage<Owner> ownerPage(@RequestBody PageSet pageSet) {
        IPage<Owner> ownerIPage = new Page<>(pageSet.getCurrent(), pageSet.getSize());
        ownerIPage = ownerService.page(ownerIPage);
        return ownerIPage;
    }

    @PostMapping("deleteowner")
    public String deleteOwner(String ownerId) {
        boolean bool = ownerService.removeById(ownerId);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("detailowner")
    public Owner detailOwner(String ownerId) {
        Owner owner = ownerService.getById(ownerId);
        return owner;
    }

    //查找
    @PostMapping("searchowner")
    public List<Owner> searchVisitor(String keyword) {
        //正则表达式验证是否为手机号
        String regex = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))[0-9]{8}$";
        //创建一个条件（为了条件查询语句）
        QueryWrapper<Owner> wrapper = new QueryWrapper<>();
        //正则表达式查询结果
        boolean bool = keyword.matches(regex);
        //物业管理的表示，判断是否根据id查找
        PropertyTage propertyTage = propertyTageService.getById(1);
        //根据id查询
        if (keyword.length() >= 4 && keyword.substring(0, 4).equals(propertyTage.getTage())) {
            //sql语句(where VisitorId = keyword);
            wrapper.lambda().eq(Owner::getOwnerId, keyword);
            List<Owner> owners = ownerService.list(wrapper);
            return owners;
        } else if (bool) {
            //sql语句(where VisitorPhone = keyword);
            wrapper.lambda().eq(Owner::getOwnerPhone, keyword);
            List<Owner> owners = ownerService.list(wrapper);
            return owners;
        } else {
            //sql语句(where VisitorName = keyword);
            wrapper.lambda().eq(Owner::getOwnerName, keyword);
            List<Owner> owners = ownerService.list(wrapper);
            return owners;
        }
    }

    @PostMapping("getparking")
    public List<Object> getParking(String managecarId){
        List<Object> list = new ArrayList<>();
        ManageCar manageCar = manageCarService.getById(managecarId);
        Owner owner = ownerService.getById(manageCar.getOwnerId());

        list.add(manageCar);
        list.add(owner);
        return list;
    }

    @PostMapping("setparking")
    public String setParking(@RequestBody ManageCar manageCar){
        boolean bool = manageCarService.updateById(manageCar);
        if (bool)
            return "success";
        else
            return "error";
    }
}
